Get N copies of the first 2 charactersΒΆ
Get the N (non-negative integer) copies of the first 2 characters of a given string.
Return the N copies of the whole string if the length is less than 2.
def substring_copy(str, N):
flen = 2
if flen > len(str):
flen = len(str)
substr = str[:flen]
result = ""
for i in range(N):
result = result + substr
return result
print(substring_copy('abcdef', 2))
print(substring_copy('p', 3));
Output:
abab
ppp